﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using utils;

namespace dal.cgm_production_dao
{
    [DataContract]
    public class MacAddressPool
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string mac_address { get; set; }
        [DataMember]
        public DateTime create_time { get; set; }
        [DataMember]
        public DateTime update_time { get; set; }
        [DataMember]
        public int is_allocated { get; set; }
        [DataMember]
        public int batche { get; set; }
    }

    public class MacAddressPoolDao
    {
        public static IList<MacAddressPool> GetMacs(int quantity)
        {
            string sql = @"SELECT id, mac_address, create_time, is_allocated, batche
                           FROM mac_address_pool
                           WHERE is_allocated=1
                           ORDER BY id ASC
                           LIMIT @quantity";

            var parameters = new MySqlParameter[]
            {
                new MySqlParameter("@quantity", quantity)
            };

            DataSet ds = MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters);

            IList<MacAddressPool> list = DataContainerUtil.DataTableToList<MacAddressPool>(ds.Tables[0]);

            return list.Any() ? list : new List<MacAddressPool>();
        }

        public static int MacsCount()
        {
            try
            {
                string sql = @"SELECT count(1) as Count
                       FROM mac_address_pool
                       WHERE is_allocated=1";

                DataSet ds = MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql);

                // 提取 DataSet 中的数量
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    return Convert.ToInt32(ds.Tables[0].Rows[0]["Count"]);
                }
            }
            catch (Exception ex)
            {
                // 捕获异常并记录日志
                ApiLog.Error("Error in MacsCount: " + ex.Message, ex);
            }

            // 如果发生异常或没有记录，返回 0
            return 0;
        }

        // 批量更新方法
        public static void UpdateMacs(List<string> macAddresses)
        {
            if (macAddresses == null || macAddresses.Count == 0)
            {
                return;
            }

            string sql = "UPDATE mac_address_pool SET is_allocated = 2, update_time = @updateTime WHERE mac_address IN (";

            // 创建SQL参数
            var parameters = new List<MySqlParameter>();
            for (int i = 0; i < macAddresses.Count; i++)
            {
                sql += i == 0 ? "@mac" + i : ", @mac" + i;
                parameters.Add(new MySqlParameter("@mac" + i, macAddresses[i]));
            }

            sql += ")";

            // 添加 updateTime 参数
            parameters.Add(new MySqlParameter("@updateTime", DateTime.Now));

            MySqlHelper.ExecuteNonQuery(ConfigString.cgm_production, sql, parameters.ToArray());
        }
    }
}
